/**
 * 登录用户处理
 * @author 1874
 */
package luohao.application.handler;

import luohao.application.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class UserInfoHandlerCopy {
    public static final Integer SUPER_ADMIN_ROLE_ID = 0; // 超级管理员角色ID值
    public static final String ADMIN_TOKEN = "admin-token"; // token标识
    public static final String ID = "id"; // 登录管理员ID
    public static final String NAME = "name"; // 姓名
    public static final String ACCOUNT = "account"; // 账号
    public static final String HEADER_IMG = "headerImg"; // 头像地址
    public static final String ROLE_ID = "roleId"; // 角色ID
    public static final String IP = "ip"; // 登录ip
    private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    @Value("${user.key-prefix}")
    private String keyPrefix; // 用户信息缓存key
    @Resource
    private RedisUtil redisUtil;
    /**
     * 获取登录用户信息
     */
    public static Map<String, Object> getUserInfoMap() {
        Map<String, Object> userInfoMap = threadLocal.get();
        // 如果线程map为空，则添加一个空map
        if (userInfoMap == null || userInfoMap.isEmpty()) {
            userInfoMap = new HashMap<>();
            threadLocal.set(userInfoMap);
        }
        return userInfoMap;
    }
    /**
     * 将map数据添加到线程中用户信息中
     */
    public static void setUserInfoMap(Map<String, Object> userInfoMap) {
        threadLocal.set(userInfoMap);
    }
    /**
     * 获取线程中用户信息key对应的值
     */
    public static Object get(String key) {
        Map<String, Object> userInfoMap = getUserInfoMap();
        return userInfoMap.get(key);
    }
    /**
     * 设置线程中用户信息key的值
     */
    public static Object set(String key, Object value) {
        Map<String, Object> userInfoMap = getUserInfoMap();
        return userInfoMap.put(key, value);
    }
    /**
     * 删除线程map
     */
    public static void deleteUserInfoMap() {
        threadLocal.remove();
    }
}
